#include "aircraft.h"
#include "sprite.h"
#include "bullet.h"
#include "game.h"

AirCraft::AirCraft(Game *g) : ControllableActor(g){
}

void AirCraft::do_action(ControllableObject::action_t act, int magnitude){
    current_action = act;
    switch (act){
        case DOWN:
            y+=4;
            break;
        case UP:
            y-=4;
            break;
        case LEFT:
            x-=4;
            break;
        case RIGHT:
            x+=4;
            break;
        case SHOOT:
            shoot_bullet();
            break;
    }
    if (x<0+get_w()/2) x=get_w()/2;
    if (x>SCREEN_W-get_w()/2) x=SCREEN_W-get_w()/2;
    if (y<0+get_h()/2) y=get_h()/2;
    if (y>SCREEN_H-get_h()) y=SCREEN_H-get_h();

    /*Animacion dependiendo la tecla que presiono*/
    Sprite *tmp;
    tmp = dynamic_cast<Sprite *>(agraph);
    tmp->update(get_current_action());
}

void AirCraft::hit(Actor *a, int damage)
{
/*    Explosion *exp=new Explosion(game, 10);
    CircleWithZoom *cwz=new CircleWithZoom(exp, 10);
    exp->set_x(a->get_x()-10+rand()%20);
    exp->set_y(a->get_y()-10+rand()%20);
    exp->set_actor_graphic(cwz);
    exp->set_is_detected(false);
    game->actor_manager->add(exp);
*/
}


void AirCraft::update(){
    Sprite *tmp;
    tmp = dynamic_cast<Sprite *>(agraph);
    /*Animacion estandar*/
    tmp->update();
}


ControllableObject::action_t AirCraft::get_current_action(){
    return current_action;
}

void AirCraft::shoot_bullet(){
    Bullet *bull = new Bullet(this->game, this);
    BITMAP *bull_bmp = load_bitmap("bullet_Selected.pcx", NULL);
    Bitmap *bull_sp = new Bitmap(bull, bull_bmp);
    //bull_sp->add_frame(bull_bmp, bull_bmp->w/2, bull_bmp->h/2, 10);
    int bullet_x = this->get_graph_x() + (this->get_w()/2);
    bull->set_x(bullet_x);
    bull->set_y(this->get_graph_y() - (this->get_h()/2));
    bull->set_actor_graphic(bull_sp); //el sprite va a ser mi actorGraphic
    bull->set_is_detected(true);
    bull->set_team(Game::ALLY);
    bull->set_collision_method(CollisionManager::PP_COLLISION);
    game->actor_manager->add(bull);

    destroy_bitmap(bull_bmp);
}
